<template>
  <div>
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品列表</el-breadcrumb-item>
    </el-breadcrumb>

    <el-card>
      <el-row :gutter="20">
        <el-col :span="8">
          <el-input
            placeholder="请输入内容"
            clearable
            v-model="queryInfo.query"
            @clear="getGoodsList"
          >
            <el-button
              slot="append"
              icon="el-icon-search"
              @click="handleCurrentChange(1);getGoodsList;"
            ></el-button>
          </el-input>
        </el-col>
        <el-col :span="4">
          <el-button type="primary" @click="goAddPage">添加商品</el-button>
        </el-col>
      </el-row>

      <el-table :data="goodslist" stripe border style="width: 100%">
        <el-table-column type="index"> </el-table-column>
        <el-table-column prop="cname" label="商品名称" width="320px"></el-table-column>
        <el-table-column prop="price" label="商品价格（元）"></el-table-column>
        <el-table-column prop="count" label="商品数量"></el-table-column>
        <el-table-column prop="model" label="商品类型"></el-table-column>
        <el-table-column prop="detail" label="商品简介" width="320px">
        </el-table-column>
        <el-table-column label="操作" width="230px">
          <template v-slot="scope">
            <el-button
              size="mini"
              type="primary"
              icon="el-icon-edit"
              @click="showEditDialog(scope.row.id)"
            ></el-button>
            <el-button
              size="mini"
              type="warning"
              icon="el-icon-delete"
              @click="removeById(scope.row.id)"
            ></el-button>
          </template>
        </el-table-column>
      </el-table>

      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pagenum"
        :page-sizes="[10, 20, 50]"
        :page-size="queryInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        background
      >
      </el-pagination>
    </el-card>
    <!-- 修改商品对话框 -->
        <el-dialog
                title="修改商品"
                :visible.sync="editDialogVisible"
                width="50%"
                @close="editDialogClosed"
        >
            <el-form
                    :model="editGoodForm"
                    ref="editGoodFormRef"
                    label-width="70px"
                    class="demo-ruleForm"
            >
                <el-form-item label="商品名称">
                    <el-input v-model="editGoodForm.cname" disabled></el-input>
                </el-form-item>
                <el-form-item label="商品数量">
                    <el-input v-model="editGoodForm.count"></el-input>
                </el-form-item>
                <el-form-item label="商品价格" prop="price">
                    <el-input v-model="editGoodForm.price"></el-input>
                </el-form-item>
                <el-form-item label="商品简介" prop="detail">
                    <el-input v-model="editGoodForm.detail"></el-input>
                </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
        <el-button @click="editDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="editGood">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      // 请求数据
      queryInfo: {
        query: "",
        pagenum: 1,
        pagesize: 10
      },
      goodslist: [],
      total: 0,
      dialogVisible: false,
      editDialogVisible: false,
      editGoodForm: {},
    };
  },
  methods: {
    async getGoodsList() {
      const { data } = await this.$http.get("mgoods/findAllByParams", {
        params: this.queryInfo
      });
      console.log(data);
      if (data.meta.status !== 200) {
        return this.$message.error(data.meta.msg);
      }
      console.log(data);
      this.goodslist = data.data.content;
      this.total = data.data.totalElements;
    },
    handleSizeChange(newSize) {
      this.queryInfo.pagesize = newSize;
      this.getGoodsList();
    },
    handleCurrentChange(newPage) {
      this.queryInfo.pagenum = newPage;
      this.getGoodsList();
    },
    async showEditDialog(id) {
                const {data} = await this.$http.post(`commodity/select/id?id=${id}`);
                console.log(data)
                if (data.meta.status !== 200) {
                    return this.$message.error("查询商品失败！");
                }
                this.editGoodForm = data.data;
                this.editDialogVisible = true;
            },
            editDialogClosed() {
                this.$refs.editGoodFormRef.resetFields();
            },
            editGood() {
                this.$refs.editGoodFormRef.validate(async valid => {
                    if (!valid) return;
                    const {data} = await this.$http.post('commodity/update', this.editGoodForm);
                    if (data.meta.status !== 200) {
                        return this.$message.error("修改商品信息失败！");
                    }
                    this.$message.success("修改成功！");
                    this.editDialogVisible = false;
                    this.getGoodsList();
                });
            },
  //根据主键删除
    removeById(id) {
      this.$confirm("此操作将永久删除该商品, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(async () => {
          const { data } = await this.$http.delete(`mgoods/delete?id=${id}`);
          if (data.meta.status !== 200) {
            return this.$message.error(data.meta.msg);
          }
          this.getGoodsList();
          this.$message({
            type: "success",
            message: "删除成功!"
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除"
          });
        });
    },
    goAddPage() {
      this.$router.push("goods/add");
    }
  },
  created() {
    this.getGoodsList();
  }
};
</script>

<style lang="less" scoped></style>